﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PocketBook.Domain.Abstract;
using PocketBook.Domain.Entities;
using PocketBook.UI.ViewModel.Workspaces;

namespace PocketBook.UI.Model
{
    public class ModelFactory : IModelFactory
    {
        private IRepository _repository;

        public ModelFactory(IRepository repository)
        {
            _repository = repository;
        }

        #region Expenses
        
        public List<Expense> GetExpenses()
        {
            return _repository.GetExpenses();
        }

        public void AddOrUpadateExpense(ExpensesWorkspaceViewModel expenses, Expense derivedExpense)
        {
            if (expenses.ExpensesList.Select(n => n.ExpenseId).Contains(derivedExpense.ExpenseId))
            {
                // Edit
                var expense = expenses.ExpensesList.SingleOrDefault(n => n.ExpenseId == derivedExpense.ExpenseId);
                expenses.ExpensesList.Remove(expense);
                expense.Name = derivedExpense.Name;
                expense.Amount = derivedExpense.Amount;
                expense.Description = derivedExpense.Description;
                expense.Date = DateTime.Now;
                _repository.UpdateExpense(expense);

                expenses.ExpensesList.Add(expense);
            }
            else
            {
                //New
                var expense = _repository.AddExpense(new Expense
                {
                    ExpenseId = derivedExpense.ExpenseId,
                    Name = derivedExpense.Name,
                    Amount = derivedExpense.Amount,
                    Description = derivedExpense.Description,
                    Date = DateTime.Now
                });

                expenses.ExpensesList.Add(expense);
            }
        }

        public void DeleteExpense(Guid id)
        {
            _repository.DeleteExpanse(id);
        } 

        #endregion

        #region Incomes
        
        public List<Income> GetIncomes()
        {
            return _repository.GetIncomes();
        }

        public void AddOrUpadateIncome(IncomesWorkspaceViewModel incomes, Income derivedIncome)
        {
            if (incomes.IncomesList.Select(n => n.IncomeId).Contains(derivedIncome.IncomeId))
            {
                // Edit
                var income = incomes.IncomesList.SingleOrDefault(n => n.IncomeId == derivedIncome.IncomeId);
                incomes.IncomesList.Remove(income);
                income.Name = derivedIncome.Name;
                income.Amount = derivedIncome.Amount;
                income.Description = derivedIncome.Description;
                income.Date = DateTime.Now;
                _repository.UpdateIncome(income);

                incomes.IncomesList.Add(income);
            }
            else
            {
                //New
                var income = _repository.AddIncome(new Income
                {
                    IncomeId = derivedIncome.IncomeId,
                    Name = derivedIncome.Name,
                    Amount = derivedIncome.Amount,
                    Description = derivedIncome.Description,
                    Date = DateTime.Now
                });

                incomes.IncomesList.Add(income);
            }
        }

        public void DeleteIncome(Guid id)
        {
            _repository.DeleteIncome(id);
        } 

        #endregion

        #region Status

        public decimal GetPocketBookStatus()
        {
            return this.GetIncomes().Sum(n => n.Amount) - this.GetExpenses().Sum(n => n.Amount);
        }

        public decimal GetPocketBookLastMonthExpenses()
        {
            return this.GetExpenses().Sum(n => n.Amount);
        }

        public decimal GetPocketBookLastMonthIncomes()
        {
            return this.GetIncomes().Sum(n => n.Amount);
        }

        #endregion
    }
}
